#include <iostream>
#include <fstream>
#include <string>
#include <map>

#include "movies_xml.hpp"
#include "people_xml.hpp"

using namespace std;

bool peop_cmp(const string& a, const string& b)
{
  return (strcasecmp(a.c_str(), b.c_str()) < 0);
}

int main(int argc, char ** argv)
{
  bool gen_movies = true;
  bool gen_people = true;
  bool gen_aka_movies = false;
  bool gen_aka_people = false;
  
  for(int a = 1; a < argc; ++a) {
    if (string(argv[a]) == "--no-movies")
      gen_movies = false;
    else if (string(argv[a]) == "--no-people")
      gen_people = false;
    else if (string(argv[a]) == "--aka-movies")
      gen_aka_movies = true;
    else if (string(argv[a]) == "--aka-people")
      gen_aka_people = true;
    else if (string(argv[a]) == "--help") {
      cout << "Usage: " << argv[0] << " [options]" << endl;
      cout << "Available options:" << endl;
      cout << "  --no-movies: Movies XML file is not generated." << endl;
      cout << "  --no-people: People XML file is not generated." << endl;
      cout << "  --aka-movies: Movies a.k.a. titles are generated. " << endl;
      cout << "  --aka-people: People a.k.a. names are generated." << endl;
      return 0;
    }
  }

  map<string, Movie> movies;
  if (gen_movies) {
    if ( parse_movies(movies) && parse_genres(movies) &&
	 (!gen_aka_movies || (gen_aka_movies && parse_aka_titles(movies))) && 
	 gen_movies_xml(movies) )
      cout << MOVIES_XML << " created!" << endl;
    else
      cout << "Error creating " << MOVIES_XML << " file!" << endl;
  } else {
    if ( !parse_movies(movies) ) {
      cout << "Error parsing movies" << endl;
    }
  }

  //movies.clear();
  
  if (gen_people) {
    map<string, Person, bool(*)(const string&,const string&)> people(peop_cmp);
    if ( parse_all_people(people,movies) && (!gen_aka_people || 
	 (gen_aka_people && parse_aka_names(people))) && gen_people_xml(people) )
      cout << PEOPLE_XML << " created!" << endl;
    else
      cout << "Error creating " << PEOPLE_XML << " file!" << endl;
  }
  
  return 0;
}
